package com.tanhua.server.service;

import com.tanhua.domain.mongo.Option;
import com.tanhua.domain.mongo.Question;
import com.tanhua.domain.mongo.Questionnarie;
import com.tanhua.domain.vo.QuestionListVo;
import com.tanhua.dubbo.api.MongoQuestionApi;
import com.tanhua.dubbo.api.OptionApi;
import com.tanhua.dubbo.api.QuestionnarieApi;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TestSoulService {

    @Reference
    private QuestionnarieApi questionnarieApi;

    @Reference
    private MongoQuestionApi mongoQuestionApi;

    @Reference
    private OptionApi optionApi;

    //【项目实战】问题列表业务
    public ResponseEntity<Object> showQuestion() throws InvocationTargetException, IllegalAccessException {
        List<Questionnarie> list = questionnarieApi.findAll();
        List<QuestionListVo> vos = new ArrayList<QuestionListVo>();
        if (list != null) {
            for (Questionnarie questionnarie : list) {
                ObjectId qid = questionnarie.getId();
                List<Question> qList = mongoQuestionApi.findByQid(qid);
                List<Map<String, Object>> questions = new ArrayList<>();

                if (qList != null) {
                    for (Question question : qList) {
                        ObjectId qqId = question.getId();
                        List<Option> oList = optionApi.findByQid(qqId);
                        List<Map<String, String>> options = new ArrayList<>();

                        if (oList != null) {
                            for (Option option : oList) {
                                Map<String, String> map = new HashMap<String, String>();
                                map.put("id", option.getId().toString());
                                map.put("option", option.getContext());
                                options.add(map);
                            }
                        }
                        Map<String, Object> ma = new HashMap<>();
                        ma.put("id", question.getId().toString());
                        ma.put("question", question.getText());
                        ma.put("options", options);
                        questions.add(ma);
                    }

                }
                QuestionListVo vo = new QuestionListVo();
                BeanUtils.copyProperties(vo, questionnarie);
                vo.setQuestions(questions);
                vo.setId(questionnarie.getId().toString());

                if (questionnarie.getLevel().equals("初级")) {
                    vo.setStar(2);
                    vo.setIsLock(0);
                    vo.setName("初级测试题");
                } else if (questionnarie.getLevel().equals("中级")) {
                    vo.setStar(3);
                    vo.setIsLock(1);
                    vo.setName("中级测试题");
                } else {
                    vo.setStar(5);
                    vo.setIsLock(1);
                    vo.setName("高级测试题");
                }

                vo.setReportId("123");

                if (vo.getId().equals("1")) {
                    vo.setIsLock(0);
                }
                vos.add(vo);

            }
        }

        return ResponseEntity.ok(vos);
    }
}
